<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('查询视图') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('脚本')">
        <el-input
          v-model="queryParams.scriptName"
          :placeholder="$t('脚本')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('名称')">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:searchview:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <!--      <el-col :span="1.5">
              <el-button
                v-hasPermi="['system:searchview:edit']"
                type="success"
                icon="el-icon-finished"
                size="mini"
                @click="allUpdate"
              >{{ $t("一键更新") }}</el-button>
            </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="baseList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('脚本')" align="center" prop="sqlBaseId">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.scriptName) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('名称')" align="center" prop="name">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.name) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('颜色')" align="center" prop="color" width="100px">
        <template slot-scope="scope">
          <span class="colorLump" :style="{'background':scope.row.color}" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:searchview:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['system:searchview:edit']"
            size="small"
            type="text"
            icon="el-icon-success"
            @click="arrangeHandle(scope.row)"
          >{{ $t('部署') }}</el-button>
          <el-button
            v-hasPermi="['system:searchview:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改高级查询sql对话框 -->
    <el-dialog :title="$t('查询视图')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <searchDefaultViewForm ref="searchDefaultViewForm" />
    </el-dialog>

    <el-dialog :title="$t('部署')" :visible.sync="deploymentOpen" :close-on-click-modal="false" :width="'50%'" :lock-scroll="false" append-to-body>
      <div v-loading="loading">
        <el-form ref="form" :model="deploymentForm" :rules="deploymentFormRules" label-width="120px" class="dataForm">
          <div class="area-header">
            <span class="area-header-title">{{ $t('默认设置') }}</span>
          </div>
          <el-row>
            <el-col :span="12">
              <el-form-item :label="$t('名称设置')" prop="isOverwriteName">
                <el-select v-model="deploymentForm.isOverwriteName" style="width: 100px;">
                  <el-option :label="$t('跳过')" value="skip" />
                  <el-option :label="$t('更新')" value="update" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('颜色设置')" prop="isOverwriteColor">
                <el-select v-model="deploymentForm.isOverwriteColor" style="width: 100px;">
                  <el-option :label="$t('跳过')" value="skip" />
                  <el-option :label="$t('更新')" value="update" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="area-header">
            <span class="area-header-title">{{ $t('筛选范围') }}</span>
          </div>
          <el-row>
            <el-col :span="12">
              <el-form-item :label="$t('筛选范围')" prop="isOverwriteScreen">
                <el-select v-model="deploymentForm.isOverwriteScreen" style="width: 100px;">
                  <el-option :label="$t('跳过')" value="skip" />
                  <el-option :label="$t('更新')" value="update" />
                </el-select>
              </el-form-item>
            </el-col>
            <!--            <el-col :span="12">-->
            <!--              <el-form-item :label="$t('若用户拥有相同类型筛选范围时')" prop="isSynchronousScreen">-->
            <!--                <el-select v-model="deploymentForm.isSynchronousScreen" :disabled="deploymentForm.isOverwriteScreen !== 'update'" style="width: 100px;">-->
            <!--                  <el-option :label="$t('跳过')" value="skip" />-->
            <!--                  <el-option :label="$t('覆盖')" value="cover" />-->
            <!--                </el-select>-->
            <!--              </el-form-item>-->
            <!--            </el-col>-->
          </el-row>
          <div class="area-header">
            <span class="area-header-title">{{ $t('展示字段') }}</span>
          </div>
          <el-row>
            <el-col :span="12">
              <el-form-item :label="$t('展示字段')" prop="isOverwriteField">
                <el-select v-model="deploymentForm.isOverwriteField" style="width: 100px;">
                  <el-option :label="$t('跳过')" value="skip" />
                  <el-option :label="$t('更新')" value="update" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="deploymentForm.isOverwriteField === 'update'" :span="12">
              <el-form-item :label="$t('更新范围')" prop="fieldUpdateScope">
                <el-select v-model="deploymentForm.fieldUpdateScope" multiple style="width: 100px;">
                  <el-option :label="$t('是否展示')" value="ZS" />
                  <el-option :label="$t('是否固定')" value="GD" />
                  <el-option :label="$t('宽度')" value="KD" />
                  <el-option :label="$t('排序')" value="PX" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align: right; margin-top: 20px;">
          <el-button @click="deploymentOpen = false">{{ $t('取消') }}</el-button>
          <el-button type="primary" @click="submitDeployment">{{ $t('保存') }}</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSearchDefaultView,
  executeAllUpdate,
  submitDefaultViewDeployment, delSearchDefaultView
} from '@/api/system/search/searchDefaultView'
import searchDefaultViewForm from './searchDefaultViewForm.vue'
// import { allListSearchSqlBase } from '@/api/system/search/searchSqlBase'
export default {
  name: 'SearchDefaultViewList',
  components: {
      searchDefaultViewForm
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 高级查询sql表格数据
          baseList: [],
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              scriptName: undefined,
              name: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          },
          deploymentOpen: false,
          deploymentForm: {
            viewId: undefined,
            code: undefined,
            isOverwriteName: undefined,
            isOverwriteColor: undefined,
            isOverwriteScreen: undefined,
            isSynchronousScreen: undefined,
            isOverwriteField: undefined,
            fieldUpdateScope: []
          },
          deploymentFormRules: {
            viewId: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }],
            code: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }],
            isOverwriteName: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }],
            isOverwriteColor: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }],
            isOverwriteScreen: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }],
            isSynchronousScreen: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }],
            isOverwriteField: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }],
            fieldUpdateScope: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }]
          }
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询高级查询sql列表 */
    getList() {
        this.loading = true
        listSearchDefaultView(this.queryParams).then(response => {
            this.baseList = response.rows
            // allListSearchSqlBase().then(res => {
            //   res.data.forEach(element => {
            //     response.rows.forEach(row => {
            //        if (element.code === row.sqlBaseId) {
            //          row.sqlBaseId = element.name
            //        }
            //     })
            //    })
            // })
            this.total = response.total
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.queryParams = {
              scriptName: undefined,
              name: undefined,
              pageNum: 1,
              pageSize: 10
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
         this.queryParams = {
              scriptName: undefined,
              name: undefined,
              pageNum: 1,
              pageSize: 10
        }
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.searchDefaultViewForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.searchDefaultViewForm.init(row)
        })
    },
    // 全部更新
    allUpdate() {
     this.$confirm('确定全部更新吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            this.loading = true
           this.$message({
              message: '正在更新，请勿操作',
              type: 'warning',
              duration: 5000
            })
        executeAllUpdate().then(res => {
            console.log(res)
            if (res.code === 200) {
             this.$message('更新完成')
            }
            this.loading = false
        })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    /** 部署按钮操作 */
    arrangeHandle(row) {
      this.deploymentForm = {
        viewId: row.id,
        code: row.sqlBaseId,
        isOverwriteName: 'update',
        isOverwriteColor: 'update',
        isOverwriteScreen: 'update',
        isSynchronousScreen: 'skip',
        isOverwriteField: 'update',
        fieldUpdateScope: ['ZS', 'GD', 'KD', 'PX']
      }
      this.deploymentOpen = true
    },
    /** 执行部署 */
    submitDeployment: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          submitDefaultViewDeployment(this.deploymentForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('部署成功'))
              this.deploymentOpen = false
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delSearchDefaultView(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    }
    /** 导出按钮操作 */
    // exportHandle() {
    //     const queryParams = this.queryParams
    //     this.$confirm('是否确认导出所有高级查询sql数据项?', this.$t('警告'), {
    //         confirmButtonText: this.$t('确定'),
    //         cancelButtonText: this.$t('取消'),
    //         type: 'warning'
    //     }).then(function() {
    //         return exportSearchSqlBase(queryParams)
    //     }).then(response => {
    //         this.download(response.msg)
    //     }).catch(function() {
    //     })
    // }
  }
}
</script>
<style lang="scss" scoped>
.colorLump{
  width:30px;
  height:30px;
  border-radius: 50%;
  display: block;
  margin: 0 auto;
}
</style>
